package com.cms.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrCustomerFieldDef;
 
import com.cms.service.CustomerFieldDefService;
import com.cms.service.ReportService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Controller
public class CustomerFieldDefControrller extends BaseController{

	
	 @Autowired
	 private ReportService reportService;
	 
	 
	 @Autowired
	 private CustomerFieldDefService customerFieldDefService;
	 
	 /***
	  * 
	  * @param req
	  * @param rep
	  * @param map
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping("/customerFieldDef.do")
	 public String enterCustomerField(HttpServletRequest req,HttpServletResponse rep,ModelMap map)throws Exception
	 {
		 Map<String,Object>param = new HashMap<String, Object>();
		 param.put(CmsConstant.CATALOG_NAME, "CUSTOMER_CATALOG");
		 map.put("customerCatalogs", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		 return "customerFieldDef/customerFieldDefMain.html";
	 }
	 
	 
	 /***
	  * 
	  * @param req
	  * @param rep
	  * @param map
	  * @return   
	  * @throws Exception
	  */
	 @RequestMapping("/customerFieldDefList.do")
	 public String customerFieldList(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 { 
		 Map<String,Object>param =this.buildQueryMap(req);
		 map.put("customerFieldDefList", this.reportService.getReportResult("queryCustomerFieldDef", param));
		 return "customerFieldDef/customerFieldDefList.html";
	 }
	 
	 
	 @RequestMapping("/saveCustomerFieldDefList.do")
	 /***
	  * 
	  * 
	  * 
	  * @author huang
	  */
	 public void customerFieldDefListDef(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {  
		 String data = req.getParameter("data");
		 String customerCatalog = req.getParameter("customerCatalog");
		 TrAdminUser tu = this.getLoginUser(req);
		 Map<String,Object>result = new HashMap<String, Object>();
		 try
		 {
		 if(!StringUtils.isBlank(data))
		 {
			JsonArray array=  new JsonParser().parse(data).getAsJsonArray();
			List<TrCustomerFieldDef>customerFieldDefs = new ArrayList<TrCustomerFieldDef>();
			TrCustomerFieldDef def = null;
			JsonObject obj = null;
			for(int i=0;array!=null&&i<array.size();i++)
			{
				obj=array.get(i).getAsJsonObject();
				def = new TrCustomerFieldDef();
				def.setCatalog(customerCatalog);
				def.setCreateUser(tu.getId());
				def.setModifyUser(tu.getId());
				def.setSeq(obj.get("seq").getAsInt());
				def.setLabel(obj.get("name").getAsString());
				def.setValue(obj.get("value").getAsString());
				customerFieldDefs.add(def);
			}
			this.customerFieldDefService.addCustomerFieldDefs(customerCatalog,customerFieldDefs);
		 }
		 else
		 {
			 this.customerFieldDefService.deleteCustomerFieldDef(customerCatalog);
		 }
		 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 }catch (Exception e) {
			 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 result.put(CmsConstant.MSG, e.getMessage()); 
		}
		 this.renderText(resp, new Gson().toJson(result));
	 }
}
